package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.domain.institution.Institution;

public class InstServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;

	public InstServlet() {
		super();
	}

	
	public void destroy() {
		super.destroy(); 
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String m = request.getParameter("m");
		if("list".equals(m)){
			process(request,response,"/page/institution/inst_list.jsp");
		}else if("input".equals(m)){
			process(request,response,"/page/institution/inst_add.jsp");
		}else if("del".equals(m)){
			del(request,response);
		}else if("add".equals(m)){
			add(request,response);
			process(request,response,"/page/institution/inst_list.jsp");
		}else if("edit".equals(m)){
			edit(request,response);
		}else if("get".equals(m)){
			process(request,response,"/page/institution/inst_edit.jsp");
		}
	}
	
	
	protected void del(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {

			String areacode = request.getParameter("id");
		//	System.out.println("areacode:"+areacode);
			
			Institution inst = new Institution();
			
			inst.delInst(areacode);
			process(request, response, "/page/institution/inst_list.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String areacode = request.getParameter("areacode");
			String agencode = request.getParameter("agencode");
			String agename = request.getParameter("agenname");
			
			Institution inst = new Institution();
			int grade=areacode.length()==6?1:2;
			inst.addInst(areacode, agencode,agename, grade);
		} catch (Exception e) {
			e.printStackTrace();
			
		}
	}
	
	private void edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try{
			String areacode = request.getParameter("areacode");
			System.out.println("areacode:"+areacode);
			String agencode = request.getParameter("agencode");
			System.out.println("agencode:"+agencode);
			String agenname = request.getParameter("agenname");
			System.out.println("agenname:"+agenname);
			
			//实例化Institution
			Institution inst = new Institution(areacode);
			inst.editInst(agencode,agenname);
			
			process(request,response,"/page/institution/inst_list.jsp");
		}catch(Exception e){
			e.printStackTrace();
		}
	}


}
